/*
 * INCLUDES (����ͷ�ļ�)
 */
#include <stdio.h>
#include <string.h>

#include "gap_api.h"
#include "gatt_api.h"
#include "gatt_sig_uuid.h"
#include "ANCS_AMS_client.h"
#include "AMS_client.h"
#include "system_fr30xx.h"
#include "FreeRTOS.h"
#include "lvgl.h"
#include "lv_user_sqlist.h"
#include "user_ANCS_info_intergration.h"
#include "app_lvgl.h"

#define NTF_ATT_ID_APPLE        0
#define NTF_ATT_ID_TITLE        1
#define NTF_ATT_ID_SUBTITLE     2
#define NTF_ATT_ID_MSG          3
#define NTF_ATT_ID_MSG_SIZE     4
#define NTF_ATT_ID_DATE         5
#define NTF_ATT_ID_POSITIVE_ACT 6
#define NTF_ATT_ID_NEGATIVE_ACT 7

char *const msg_type_string[]=
{
#if 0
    [CATEGORY_WEIXIN]="com.tencent.xin",
    [CATEGORY_MOBILE_SMS]="com.apple.MobileSMS",
    [CATEGORY_MOBILE_PHONE]="com.apple.mobilephone",
    [CATEGORY_MQQ]="com.tencent.mqq",
    [CATEGORY_QQ]="com.tencent.qq",
    [CATEGORY_WHATSAPP]="net.whatsapp.WhatsApp",
    [CATEGORY_FACEBOOK]="com.facebook.Facebook",
    [CATEGORY_LINE]="jp.naver.line",
    [CATEGORY_FACEBOOK_MES]="com.facebook.Messenger",
    [CATEGORY_INSTAGRAM]="com.burbn.instagram",
    [CATEGORY_IPAD_QQ]="com.tencent.mipadqq",
    [CATEGORY_ZEPHYR]="com.linkedin.Zephyr",
    [CATEGORY_WEIBO]="com.sina.weibo",
    [CATEGORY_VOIP]="com.viber.voip",
    [CATEGORY_KAKAOTALK]="com.iwilab.KakaoTalk",
    [CATEGORY_DINGTALK]="com.laiwang.DingTalk",
    [CATEGORY_DINGTALKLITE]="com.alibaba.dingtalklite",
    [CATEGORY_TWITTER]="com.atebits.Tweetie2",
    [CATEGORY_TELEGRAPH]="ph.telegra.Telegraph",
#else
    [CATEGORY_WECHAT]="xin",
    [CATEGORY_MOBILE_PHONE]="phone",
    [CATEGORY_FACEBOOK]="facebook",
    [CATEGORY_FLICKR]="flickr",
    [CATEGORY_GMAIL]="gmail",
    [CATEGORY_GOOGLE_PLUS]="google",
    [CATEGORY_HANGOUTS]="hangouts",
    [CATEGORY_INS]="ins",
    [CATEGORY_KAKAO]="kakao",
    [CATEGORY_LINE]="line",
    [CATEGORY_LINK]="link",
    [CATEGORY_MESSAGE]="SMS",
    [CATEGORY_MESSENGER]="messenger",
    [CATEGORY_PINTEREST]="pinterest",
    [CATEGORY_QQ]="qq",
    [CATEGORY_SKYPE]="skype",
    [CATEGORY_SNAPCHAT]="snapchat",
    [CATEGORY_TELEGRAM]="telegram",
    [CATEGORY_TUMBLR]="tumblr",
    [CATEGORY_TWITTER]="twitter",
    [CATEGORY_VIBER]="viber",
    [CATEGORY_VKONTAKTE]="vkontakte",
    [CATEGORY_WHATSAPP]="whatsapp",
    [CATEGORY_YOUTUBE]="youtube",
    [CATEGORY_ZALO]="zalo",
#endif
};
ElemType_t msg_info_index[MAX_MSG_LIST] = {0};
SqlistType_t msg_sqlist=
{
    .length = 0,
    .data = msg_info_index,
};
extern lv_obj_t *prj_parent_cont;
extern ancs_msg_t   msglist;
//=======================================================
//=======================================================
//=======================================================
/**
 * @Function: 
 * @Descripttion: 
 * @param {uint8_t} *data
 * @return {*}1:success,0:fail
 */
bool fr_lv_message_add(uint8_t *data)
{
    uint8_t len = lv_user_list_get_sqlist_length(&msg_sqlist);
    if(len >= MAX_MSG_LIST)
        return 0;       //overflow
    //顺序查询还有哪些空闲的数组
    uint8_t elem_id,msg_info_index;
    for (uint8_t i = 0; i < MAX_MSG_LIST; i++)
    {
        msg_info_index = i | 0x80;
        elem_id = lv_user_list_get_elem_id(&msg_sqlist,msg_info_index);
        if(elem_id == 0xff)
        {
            //当前为空,将对应的下标插入队列
            lv_user_list_insert(&msg_sqlist,len,msg_info_index);
            //同时将数据写入相对应数组
            memcpy(&msglist.fifo[i],data,data[1]);
            return 1;
        }
    }
    return 0; 
}
/**
 * @Function: 
 * @Descripttion: 
 * @param {uint8_t} elem_index:队列的第elem_index个元素
 * @return {*}1:success,0:fail
 */
bool fr_lv_message_delete(uint8_t elem_index)
{
    uint8_t elem_msg_info_index = msg_sqlist.data[elem_index] & 0x7F;
    if(elem_msg_info_index >= MAX_MSG_LIST)
        return 0;
    
    if(lv_user_list_delete(&msg_sqlist,elem_index) == SUCCESS)
    {
        //清除数组里的数据
        memset(&msglist.fifo[elem_msg_info_index],0,MESSAGE_MAX_LENGTH);
        return 1;
    }
    else 
        return 0;
}

/**
 * @Function: 
 * @Descripttion: 获取到最新的信息
 * @return {*}
 */
info_msg_t *fr_lv_message_get_latest_info(void)
{
    if(msg_sqlist.length == 0)
        return NULL;
    
    uint8_t latest_index = msg_sqlist.length - 1;
    uint8_t elem_msg_info_index = msg_sqlist.data[latest_index] & 0x7F;
    return &msglist.fifo[elem_msg_info_index];
}
/**
 * @Function: 
 * @Descripttion: 获取队列中第elem_index条信息
 * @param {uint8_t} elem_index
 * @return {*}
 */
info_msg_t *fr_lv_message_get_info(uint8_t elem_index)
{
    if(elem_index >= msg_sqlist.length)
        return NULL;
    uint8_t elem_msg_info_index = msg_sqlist.data[elem_index] & 0x7F;
    return &msglist.fifo[elem_msg_info_index];
}
/**
 * @Function: 
 * @Descripttion: 获取队列中消息长度
 * @return {*}
 */
uint8_t fr_lv_message_get_info_length(void)
{
    return msg_sqlist.length;
}

/**
 * @Function: 
 * @Descripttion: 清空消息
 * @return {*}
 */
void fr_lv_message_clear_all(void)
{
    msg_sqlist.length = 0;
    memset(msg_info_index,0,sizeof(msg_info_index));
    memset(&msglist,0,sizeof(ancs_msg_t));
}

uint32_t user_test_get_mes_remind_status(void)
{
    return 0x00ffffff;
}
void user_ANCS_recv_data_src(uint8_t conidx,uint8_t *p_data, uint16_t len)
{
    uint8_t msg_type = CATEGORY_OTHER;
    uint16_t i = 0;
    uint16_t data_len = 0;
    uint8_t att_flag = 0;

    uint8_t event_id = p_data[i++];
    uint32_t uid = *(uint32_t *)(p_data + i);
    i+=4;
    printf("evt_id:%d,uid:%x,len:%d\r\n",event_id,uid,len);

    uint8_t ancs_data_len = 0;
    uint8_t *ancs_data_buff = (uint8_t *)pvPortMalloc(MESSAGE_MAX_LENGTH);
    if(ancs_data_buff == NULL)
        return;   
    memset(ancs_data_buff,0,MESSAGE_MAX_LENGTH);

    while((i < len) && (att_flag != 0xff))
    {
        switch(p_data[i++])
        {
            case NTF_ATT_ID_APPLE:
				if(	att_flag|0x01)
				{
                    data_len = *(uint16_t *)(p_data + i); 
                    i+=2;
                    uint32_t mes_type_remind_status = user_test_get_mes_remind_status();
                    if(mes_type_remind_status == 0)
                        break;
                    for (uint8_t j = 0; j < (sizeof(msg_type_string)/sizeof(char *)); j++)
                    {
                        // if( memcmp(p_data+i,msg_type_string[j],strlen(msg_type_string[j])) == 0 )
                        if( strstr((const char *)(p_data+i),(const char *)msg_type_string[j]) != NULL )
                        {
                            //is enable?
                            if(mes_type_remind_status & (1<<j))
                                msg_type = j;
                            else
                                msg_type = CATEGORY_NULL;
                            printf("msg_type:%d\r\n",msg_type);
                            break;
                        }
                    }
                    i+=data_len;
	                att_flag |= 1;  
                }
                break;

            case NTF_ATT_ID_TITLE:  //UTF-8
            	if(	att_flag|0x02)
	            {
                    data_len = *(uint16_t *)(p_data + i);
                    i+=2;

                    //date & time
                    ancs_data_len = 17;//type(1 bytes) + total_len(1 bytes) + date + time�?5 bytes //20220211T210959�?
                    if(ancs_data_len + data_len >= 64)
                    {
                        data_len = 64 - ancs_data_len;
                    }
                    ancs_data_buff[ancs_data_len] = data_len+1;//空出一个\0
                    ancs_data_len ++;
                    memcpy(ancs_data_buff+ancs_data_len,p_data + i,data_len);
                    ancs_data_len += data_len;
                    ancs_data_len ++;//空出一个\0
                    
                    i+=data_len;
	                att_flag |= 2;
                }
                break;

            case NTF_ATT_ID_SUBTITLE:   //UTF-8
                data_len = *(uint16_t *)(p_data + i);
                i+=2;
                i+=data_len;
                break;

            case NTF_ATT_ID_MSG:    //UTF-8
            	if(att_flag|0x04)
	            {
                    data_len = *(uint16_t *)(p_data + i);
                    i+=2;
                    if(ancs_data_len + data_len >= MESSAGE_MAX_LENGTH)
                    {
                        data_len = MESSAGE_MAX_LENGTH - ancs_data_len;
                    }
                    ancs_data_buff[ancs_data_len] = data_len;
                    ancs_data_len ++;
                    memcpy(ancs_data_buff+ancs_data_len,p_data + i,data_len);
                    ancs_data_len += data_len;
                    // total len
                    ancs_data_buff[1] = ancs_data_len;
                    //msg type  
                    ancs_data_buff[0] = msg_type;
                    i+=data_len;
	                att_flag |= 4;
                }
                break;

            case NTF_ATT_ID_MSG_SIZE:   //ASCII
                data_len = *(uint16_t *)(p_data + i);
                i+=2;
                i+=data_len;
                break;

            case NTF_ATT_ID_DATE:       //ASCII
            	if(att_flag|0x08)
	            {  
                    data_len = *(uint16_t *)(p_data + i);
                    i += 2;
                    memcpy(ancs_data_buff+2,p_data + i,15);
                    i += data_len;

                    att_flag |= 8;
                    // if(att_flag == 0x0f)
                    // {	
                    //     goto parse_end;
                    // }
	            }
                break;

            case NTF_ATT_ID_POSITIVE_ACT:       //UTF-8
                data_len = *(uint16_t *)(p_data + i);
                i += 2;
                i += data_len;
                break;

            case NTF_ATT_ID_NEGATIVE_ACT:       //UTF-8
                data_len = *(uint16_t *)(p_data + i);
                i += 2;
                i += data_len;
                break;

            default:
                printf("ERRR\r\n");
                break;
        }
    }
    if((att_flag == 0x0f) && (msg_type != CATEGORY_NULL))
    // if((att_flag == 0x0f))
    {
        if(ancs_data_buff[1] > 17 )
        {
            // ancs_msg_ipc_send(msg_type,ancs_data_buff,ancs_data_len);
            //show the message
            // printf("msg_type:%d\r\n",msg_type);
            // printf("ancs_data_len:%d,data:\r\n",ancs_data_len);
            // for (uint8_t m = 0; m < ancs_data_len; m++)
            // {
            //     printf("0x%x,",ancs_data_buff[m]);
            // }
            // printf("%s\r\n",ancs_data_buff);

            if(fr_lv_message_add(ancs_data_buff))
                 gui_task_msg_send(MESSAGE_IN_EVT,NULL,0,NULL,0,NULL);
        } 
    }
    vPortFree(ancs_data_buff);
}
/*
//============================================
//test add message
info_msg_t test_data5=
{
    .msg_type = 5,
    .data_len = 17+50,
    .msg_time = {"20231005"},
    .msg_context = {"\x0atest_data51a555"},
};
static uint8_t message_counter = 0;
void test_show_message(void)
{
    char test_disp_title[] = {"这是标题"};  
    // char test_disp_context[] = {"晚上一起去打球!"};
    char test_disp_context[] = {"天气0testest"};

    test_disp_context[8] = 0x30+message_counter;
    message_counter++;

    uint16_t char1_len = sizeof(test_disp_title);
    uint16_t char2_len = sizeof(test_disp_context);
    printf("size of test_disp_title:%d\r\n",char1_len);
    printf("size of test_disp_context:%d\r\n",char2_len);
    test_data5.data_len = 17+char1_len+char2_len;
    test_data5.msg_context[0] = char1_len;
    memcpy(&test_data5.msg_context[1],test_disp_title,char1_len);

    uint8_t *test_ptr = &test_data5.msg_context[char1_len+1];
    test_ptr[0] = char2_len;
    memcpy(&test_ptr[1],test_disp_context,char2_len);


    if(fr_lv_message_add(&test_data5))
    {
        printf("add new meesage!ok\r\n");
        lv_event_send(prj_parent_cont, LV_EVENT_ANCS_MSG_IN, NULL);
    }
        
}
*/


